import reqwest from "reqwest";

export const CUSTOMERS_REQUEST="CUSTOMERS_REQUEST"
export const CUSTOMERS_RECEIVE="CUSTOMERS_RECEIVE"
export const CUSTOMERS_CLEAR="CUSTOMERS_CLEAR"
export const CUSTOMERS_CHANGE_DEPARTMENT="CUSTOMERS_CHANGE_QUERY"
export const CUSTOMERS_LAST_PAGE="CUSTOMERS_LAST_PAGE"



export function customersRquest(){
  return {type:CUSTOMERS_REQUEST}
}


export function customersReceive(items){
  return {type:CUSTOMERS_RECEIVE,items}
}

export function customersLastPage(){
  return {type:CUSTOMERS_LAST_PAGE}
}

export function fetchCustomers(){
  return function(dispatch,getState){
    const state = getState()
    console.log();
    dispatch(customersRquest())
    reqwest({
      url: "customerResource/getList?userId=1&sort=a.createdTime&desc=desc",
      method: 'post',
      data:state.getIn(["customers","query"]).toJS()
    }).then(function(data){
      const {currpage,totalpages,resultList}=data.data;
      if(currpage==totalpages){
        dispatch(customersLastPage())
      }
    
      return resultList
    }).then(items=>items.map(item=>({
      contacter:item.contacter,
      customerCompanyName:item.customerCompanyName,
      area:item.area,
      rent:item.rent,
      statusName:item.statusName,
      districtName:item.districtName,
      createtime:item.createtime,
      expireDate:item.expireDate,
      lastLookTime:item.lastLookTime,
      departmentName:item.departmentName,
      employeeName:item.employeeName
    }))).then(items=>dispatch(customersReceive(items)));
  }
}
